package com.example.exam.service.impl;

import com.example.exam.entity.ExamPaper;
import com.example.exam.entity.ExamUser;
import com.example.exam.entity.User;
import com.example.exam.repository.ExamPaperRepository;
import com.example.exam.repository.ExamUserRepository;
import com.example.exam.repository.UserRepository;
import com.example.exam.service.ExamUserService;
import com.example.exam.vo.ExamPaperVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author shishuqian
 * date 2021/7/4
 * time 17:48
 **/
@Service
public class ExamUserServiceImpl implements ExamUserService {

    @Autowired
    private ExamUserRepository examUserRepository;

    @Autowired
    private ExamPaperRepository examPaperRepository;

    @Autowired
    private UserRepository userRepository;


    @Override
    public void selectPeople(Integer examPaperId, List<Integer> userIds) {

        examUserRepository.deleteAllByExamPaperId(examPaperId);
        List<ExamUser> examUsers = new LinkedList<>();
        for (Integer userId:userIds){
            ExamUser examUser = new ExamUser(examPaperId,userId,false);
            examUsers.add(examUser);
        }
        examUserRepository.saveAll(examUsers);
    }

    @Override
    public List<ExamUser> findAllByExamPaperId(Integer examPaperId) {
        return examUserRepository.findAllByExamPaperId(examPaperId);
    }

    @Override
    public List<String> findFillByExamPaperId(Integer examPaperId, Boolean filled) {

        List<ExamUser> examUserList = examUserRepository.findAllByExamPaperIdAndFilled(examPaperId, filled);
        List<String> names = new LinkedList<>();
        for (ExamUser user: examUserList){
            String realName = userRepository.findById(user.getUserId()).get().getRealName();
            names.add(realName);
        }

        return names;
    }

    @Override
    public ExamUser findByExamPaperIdAndUserId(Integer examPaperId, Integer userId) {
        return examUserRepository.findByExamPaperIdAndUserId(examPaperId,userId);
    }

    @Override
    public void setFilled(Integer examPaperId, Integer userId) {
        ExamUser examUser = examUserRepository.findByExamPaperIdAndUserId(examPaperId, userId);
        examUser.setFilled(true);
        examUserRepository.save(examUser);
    }

    @Override
    public List<ExamPaperVO> waitToDo(Integer userId) {

        List<ExamUser> examUserList = examUserRepository.findAllByUserIdAndFilled(userId, false);
        List<ExamPaperVO> voList = new LinkedList<>();
        for (ExamUser examUser:examUserList){

            ExamPaperVO vo = new ExamPaperVO();
            ExamPaper examPaper = examPaperRepository.findById(examUser.getExamPaperId()).get();
            User user = userRepository.findById(examPaper.getCreateUserId()).get();
            vo.setId(examPaper.getId());
            vo.setStartTime(examPaper.getStartTime());
            vo.setEndTime(examPaper.getEndTime());
            vo.setTitle(examPaper.getTitle());
            vo.setDescription(examPaper.getDescription());
            vo.setSuggestTime(examPaper.getSuggestTime());
            vo.setStatus(examPaper.getStatus());
            vo.setCreatorName(user.getRealName());

            voList.add(vo);
        }
        return voList;

    }
}
